package com.tg.rxjava;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func0;
import rx.observables.BlockingObservable;
import rx.schedulers.Schedulers;

/**
 *
 */
public class RxJavaDeferTest {

    public static void main(String[] args) throws Exception {

        Observable<Integer> observable = Observable.defer(
                new Func0<Observable<Integer>>() {
                    public Observable<Integer> call() {
                        System.out.println("1层绑定");

                        return Observable.defer(new Func0<Observable<Integer>>() {
                            public Observable<Integer> call() {
                                System.out.println("2层绑定");

                                return Observable.just(99);
                            }
                        });

                    }
                }

        );

        System.out.println("start...");

        observable.subscribeOn(Schedulers.io()).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("error" + e);
            }

            @Override
            public void onNext(Integer result) {
                System.out.println("result-" + result);
            }
        });

        System.out.println("end...");
        Thread.sleep(5000); //耗时的操作
    }


}
